
package com.handpay.nocardpay.baselibrary.recommendercod.view.fragment;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.text.TextUtilsCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.BaseProtocolKey;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.view.fragment.BaseFragment;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.config.SecureManager;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.recommendercod.model.bean.RecommenderCodeBean;
import com.handpay.nocardpay.baselibrary.recommendercod.presenter.RecommenderCodePresenter;
import com.handpay.nocardpay.baselibrary.recommendercod.view.IRecommenderCodeView;
import com.handpay.nocardpay.baselibrary.utils.ClientEngine;
import com.handpay.nocardpay.baselibrary.utils.CommonUtils;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.xys.libzxing.zxing.encoding.EncodingUtils;

import java.util.HashMap;

/**
 * Created by lyang on 2017/8/17.
 */

public class RecommenderCodeFragment extends BaseFragment implements IRecommenderCodeView {

    private RecommenderCodePresenter mRecommenderCodePresenter;
    private TextView recommender_code_tv;
    private ImageView qrcode_iv;

    public static RecommenderCodeFragment newInstance() {
        Bundle args = new Bundle();
        RecommenderCodeFragment fragment = new RecommenderCodeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_recommender_code;
    }

    @Override
    public void initView(View view) {
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        initToolbar(toolbar, true);
        recommender_code_tv = (TextView) view.findViewById(R.id.recommender_code_tv);
        qrcode_iv = (ImageView) view.findViewById(R.id.qrcode_iv);
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mRecommenderCodePresenter = new RecommenderCodePresenter(_mActivity, this);
        HashMap<String, Object> paramers = new HashMap<String, Object>();
        paramers.put(BaseProtocolKey.CHANNEL, ClientEngine.getInstance().CHANNEL);
        paramers.put(BaseProtocolKey.CLIENTCHANNEL, NoCardPayConfig.CLIENTCHANNEL);
        mRecommenderCodePresenter.getRecommenderCode(paramers, RecommenderCodeBean.class, true);
    }

    @Override
    public String getFragmentTitle() {
        return getString(R.string.my_recommender_code);
    }

    @Override
    public void setRecommenderCode(RecommenderCodeBean response) {
        //mmt001 aaa111
        String code = response.getRecommender();
        String url = response.getUrl();
        code = SecureManager.getInstance().des(code, 0, null);
        recommender_code_tv.setText(code);
        if (!TextUtils.isEmpty(url)) {
            url = SecureManager.getInstance().des(url, 0, null);
        }
        BitmapDrawable ic_launcher = (BitmapDrawable) this.getResources().getDrawable(R.mipmap.ic_launcher);
        Bitmap mQRCodeBitmap = EncodingUtils.createQRCode(url, CommonUtils.dp2px(180), CommonUtils.dp2px(180), ic_launcher.getBitmap());
        qrcode_iv.setImageBitmap(mQRCodeBitmap);
    }
}
